#include <iostream>
#include <vector>
using namespace std;
const int N = 1e6 + 10;
int getSum(int x)
{
    int sum = 0;
    while (x)
    {
        sum += x % 10;
        x /= 10;
    }
    return sum;
}
int main()
{
    int T;
    cin >> T;
    vector<int> stone(N, -1);
    stone[1] = 0;
    int pos = 1;
    int next;
    while (pos <= N)
    {
        next = getSum(pos) + pos;
        stone[next] = stone[pos] + 1;
        pos = next;
    }

    while (T--)
    {
        int ans;
        cin >> ans;
        cout << stone[ans] << endl;
    }
    return 0;
}